const Goods = require('../model/goods.model')
const { Op } = require("sequelize");

class GoodsService {
  async findAllGoods(pageNum, pageSize, type, category, sreach, sort, minPrice = 0, maxPrice = 1000) {

    const offset = (pageNum - 1) * pageSize
    console.log(arguments)
    let where = {}
    let order = []
    if (type) {
      where.type = type
    }
    if (category) {
      where.category = category
    }
    if (minPrice || maxPrice) {
      where.price = {
        [Op.gte]: minPrice,
        [Op.lte]: maxPrice
      }
    }
    if (sreach) {
      where.name = {
        [Op.like]: '%' + sreach + '%'
      }
    }
    if (sort) {
      order.push(['price', sort])
    }


    const { count, rows } = await Goods.findAndCountAll({
      where,
      order,
      offset: offset,
      limit: pageSize * 1
    })
    return {
      pageNum: pageNum, // 页数
      total: count, // 总条数
      rows // 结果
    }
  }
  async FindOneGoods(id) {
    return await Goods.findOne({
      where: {
        id
      }
    })
  }
}

module.exports = new GoodsService